<cfcomponent name="CFSvnFckFacade" hint="Facade to cfsvn for fckeditor">

	<cffunction name="dumpvar">
	  <cfargument name="var">
	  <cfargument name="abort" default="true">
	  <cftry>
		  <cfdump var="#var#">
		<cfcatch>
		  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
		</cfcatch>
		</cftry>
		<cfif arguments.abort>
		  <cfabort />
		</cfif>
	</cffunction>

	<cffunction name="setSvnService" access= "public" returntype="void" output="false">	
		<cfargument name="svnService" type= "any" required="true">	
		<cfset variables.svnService = arguments.svnService/>
	</cffunction>

	<cffunction name="getSvnService" access= "public" returntype="any" output="false">	
		<cfreturn variables.svnService />
	</cffunction>

	<cffunction name="setImagecfc" access= "public" returntype="void" output="false">	
		<cfargument name="Imagecfc" type= "any" required="true">	
		<cfset variables.Imagecfc = arguments.Imagecfc/>
	</cffunction>

	<cffunction name="getImagecfc" access= "public" returntype="any" output="false">	
		<cfreturn variables.Imagecfc />
	</cffunction>

	<cffunction name="init" access="public" returntype="any" hint="cfsvn object constructor.">
		<cfreturn this />
	</cffunction>

	<cffunction name="GetFoldersAndFiles" access= "public" returntype="any" output="false">
		<cfargument name="resourceURL" default="/">
	  <cfset var foldersAndFiles = getSvnService().list(resourceURL)>
	  <cfquery name="getFiles" dbtype="query">
		  SELECT * FROM foldersAndFiles
		  WHERE lower(kind) LIKE 'file'
		</cfquery>
	  <cfquery name="getFolders" dbtype="query">
		  SELECT * FROM foldersAndFiles
		  WHERE lower(kind) LIKE 'dir'
		</cfquery>
<!---
 	  <cfdump var="#foldersAndFiles#"><cfabort >
 --->
	  <cfset daFile = '<?xml version="1.0" encoding="utf-8" ?>
		<Connector command="GetFoldersAndFiles" resourceType="File">
			<CurrentFolder path="#resourceURL#" url="#resourceURL#" />
			<Folders>'>
			<cfloop query="getFolders">
				<cfset daFile = daFile & '<Folder name="#listLast(name,'/')#" />'>
			</cfloop>
			<cfset daFile = daFile & '</Folders>'>
			<cfset daFile = daFile & '<Files>'>
			<cfloop query="getFiles">
				<cfset daFileName = xmlformat("#name#")>
				<cfset daFile = daFile & '<File name="#daFileName#" size="#size#" url="#url#" />'>
			</cfloop>
			<cfset daFile = daFile & '</Files>
		</Connector>'>
	  <cfreturn daFile>
  </cffunction>

	<cffunction name="GetFolders" access= "public" returntype="any" output="false">
		<cfargument name="resourceURL" default="/">
	  <cfset var foldersAndFiles = getSvnService().list(resourceURL)>
	  <cfquery name="getFolders" dbtype="query">
		  SELECT * FROM foldersAndFiles
		  WHERE lower(kind) LIKE 'dir'
		</cfquery>
<!---
 	  <cfdump var="#foldersAndFiles#"><cfabort >
 --->
	  <cfset daFile = '<?xml version="1.0" encoding="utf-8" ?>
		<Connector command="GetFoldersAndFiles" resourceType="File">
			<CurrentFolder path="#resourceURL#" url="#resourceURL#" />
			<Folders>'>
			<cfloop query="getFolders">
				<cfset daFile = daFile & '<Folder name="#listLast(name,'/')#" />'>
			</cfloop>
			<cfset daFile = daFile & '</Folders>
		</Connector>'>
	  <cfreturn daFile>
  </cffunction>

	<cffunction name="CreateFolder" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfargument name="newFolderName" default="/">
		<cfargument name="commitMessage" default="/">
		<cfset var newFolder = arguments.currentFolder & arguments.newFolderName>
	  <cfset var daError = 0>
			<cftry>
		  <cfset newFolder = getSvnService().createDirectory(newFolder, arguments.commitMessage)>
			<!--- 	
			Possible Error Numbers are:
			o 0 : No Errors Found. The folder has been created.
			o 101 : Folder already exists.
			o 102 : Invalid folder name.
			o 103 : You have no permissions to create the folder.
			o 110 : Unknown error creating folder.
			 --->
<!---
 				<cfoutput>
					1#CFCATCH.Type#<br>
					2#CFCATCH.Message#<br>
					2#CFCATCH.detail#<br>
					3#daError#
					4#CFCATCH.ErrorCode#<br>
				</cfoutput><br>	
				<cfabort>
 --->
				<cfcatch>
					<cfif findNoCase('File already exists',cfcatch.message)>
						<cfset daError = 101>
					<cfelse>
						<cfdump var="#cfcatch#"><cfabort>
					</cfif>
				</cfcatch>
			</cftry>
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="CreateFolder" resourceType="File">
			<CurrentFolder path="#currentFolder#" url="#CurrentFolder#" />'>
			<cfset daFile = daFile & '<Error number="#daError#" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>

	<cffunction name="CreateFile" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfargument name="newFileName" default="/">
		<cfargument name="commitMessage" default="/">
		<cfset var newFolder = "">
	  <cfset var daError = 0>
	  <cfset var url = currentFolder>
			<cftry>
		  <cfset newFolder = getSvnService().addOrUpdateResource(url & newFileName,commitMessage,"Blank File")>
			<!--- 	
			Possible Error Numbers are:
			o 0 : No Errors Found. The folder has been created.
			o 101 : Folder already exists.
			o 102 : Invalid folder name.
			o 103 : You have no permissions to create the folder.
			o 110 : Unknown error creating folder.
			 --->
<!---
 				<cfoutput>
					1#CFCATCH.Type#<br>
					2#CFCATCH.Message#<br>
					2#CFCATCH.detail#<br>
					3#daError#
					4#CFCATCH.ErrorCode#<br>
				</cfoutput><br>	
				<cfabort>
 --->
				<cfcatch>
					<cfif findNoCase('File already exists',cfcatch.message)>
						<cfset daError = 101>
					</cfif>
				</cfcatch>
			</cftry>
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="CreateFile" resourceType="File">
			<CurrentFolder path="#currentFolder#" url="#CurrentFolder#" />'>
			<cfset daFile = daFile & '<Error number="#daError#" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>

	<cffunction name="iconLookup">
	  <cfargument name="mimeType" required="yes">
		<cfargument name="fileExt" required="yes">
		<cfscript>
			var iconDir = getDirectoryFromPath(getDirectoryFromPath(getCurrentTemplatePath())&"..");
			var mimeIcons = structNew();
			var extIcons = structNew();
			mimeIcons["image"] = "image.jpg";
			mimeIcons["audio"] = "sound.jpg";
			mimeIcons["video"] = "video.jpg";
			mimeIcons["text"] = "document2.jpg";
			mimeIcons["text/html"] = "html.jpg";
			mimeIcons["application"] = "binary.jpg";
			mimeIcons["application/pdf"] = "pdf.jpg";
			mimeIcons["application/msword"] = "document2.jpg";
			mimeIcons["application/postscript"] = "postscript.jpg";
			mimeIcons["application/rtf"] = "document2.jpg";
			mimeIcons["application/vnd.ms-excel"] = "document2.jpg";
			mimeIcons["application/vnd.ms-powerpoint"] = "document2.jpg";
			mimeIcons["application/x-tar"] = "tar.jpg";
			mimeIcons["application/zip"] = "tar.jpg";
			mimeIcons["message"] = "email.jpg";
			mimeIcons["message/html"] = "html.jpg";
			mimeIcons["model"] = "kmplot.jpg";
			mimeIcons["multipart"] = "kmultiple.jpg";
			
			extIcons["pdf"] = "pdf.jpg";
			extIcons["ps"] = "postscript.jpg";
			extIcons["eps"] = "postscript.jpg";
			extIcons["ai"] = "postscript.jpg";
			extIcons["ra"] = "real_doc.jpg";
			extIcons["rm"] = "real_doc.jpg";
			extIcons["ram"] = "real_doc.jpg";
			extIcons["wav"] = "sound.jpg";
			extIcons["mp3"] = "sound.jpg";
			extIcons["ogg"] = "sound.jpg";
			extIcons["eml"] = "email.jpg";
			extIcons["tar"] = "tar.jpg";
			extIcons["zip"] = "tar.jpg";
			extIcons["bz2"] = "tar.jpg";
			extIcons["tgz"] = "tar.jpg";
			extIcons["gz"] = "tar.jpg";
			extIcons["rar"] = "tar.jpg";
			extIcons["avi"] = "video.jpg";
			extIcons["mpg"] = "video.jpg";
			extIcons["mpeg"] = "video.jpg";
			extIcons["jpg"] = "image.jpg";
			extIcons["gif"] = "image.jpg";
			extIcons["png"] = "image.jpg";
			extIcons["jpeg"] = "image.jpg";
			extIcons["nfo"] = "info.jpg";
			extIcons["xls"] = "spreadsheet.jpg";
			extIcons["csv"] = "spreadsheet.jpg";
			extIcons["html"] = "html.jpg";
			extIcons["htm"] = "html.jpg";
			extIcons["doc"] = "document2.jpg";
			extIcons["rtf"] = "document2.jpg";
			extIcons["txt"] = "document2.jpg";
			extIcons["xla"] = "document2.jpg";
			extIcons["xlc"] = "document2.jpg";
			extIcons["xlt"] = "document2.jpg";
			extIcons["xlw"] = "document2.jpg";
			extIcons["txt"] = "document2.jpg";
		  if (find('/',iconDir)) { filesep = '/'; } else { filesep = '\';}
			if (mimeType neq "") {
				//Check specific cases
				mimes = structKeyList(mimeIcons);
				if (listContains(mimes,mimeType)) {
					return iconDir & "#filesep#images#filesep#" & mimeIcons[mimeType];
				} else {
					//Check for the generic mime type
					mimePrefix="text";
					if (findNoCase("/",mimeType)) { 
					  mimePrefix = listFirst(mimeType,'/');
						}
					
					if (listContains(mimes,mimePrefix)) {
						return iconDir & "#filesep#images#filesep#" & mimeIcons[mimePrefix];
					} else {
						return iconDir & "#filesep#images#filesep#empty.jpg";	
					}
				}
			} else {
				extentions = structKeyList(extIcons);
				if (listContains(extentions,fileExt)) {
					return iconDir & "#filesep#images#filesep#" & extIcons[fileExt];
				} else {
					return iconDir & "#filesep#images#filesep#empty.jpg";	
				}
			}
			return iconDir & "#filesep#images#filesep#empty.jpg";	
		</cfscript>
	
	</cffunction> 

	<cffunction name="thumbnail" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfargument name="fileName" default="/">
<!---
 	  <cfset var exportDir = getSvnService().getExportDirectory()>
<cfdump var="#iconLookup('',listLast(url.FileName,"."))#"><cfabort>
 --->
<cfcontent file="#iconLookup('',listLast(url.FileName,"."))#">
<cfabort>
			<cffile action="read" file="#iconLookup('',listLast(url.FileName,"."))#" variable="daFile">
			<cfoutput><cfcontent type="image/jpg" reset="true">#toString(daFile)#</cfoutput> 
<!--- 
		<cfhttp getasbinary="yes"	method="Get" url="#cgi.HTTP_HOST#:#cgi.SERVER_PORT#/?event=repo.resource.view&resourceUrl=#currentFolder##FileName#:-1&imgIsWrapped=1&#session.urlToken#" >
		<cfif listFirst(cfhttp.MIMEType,'/') eq 'image'>
			<cfcontent type="#cfhttp.MIMEType#" reset="true"><cfoutput>#toString(cfhttp.FileContent)#</cfoutput>
		<cfelse>
			<cffile action="read" file="#iconLookup(cfhttp.MIMEType,listLast(url.FileName,"."))#" variable="daFile">
			<cfoutput><cfcontent type="image/jpg" reset="true">#toString(daFile)#</cfoutput> 
		</cfif>
 --->
	<cfabort>
	
	</cffunction>

	<cffunction name="shrinkImageIfNeeded" access="public" returntype="any" output="false">
		<cfargument name="filepath" required="true" />
		<cfargument name="maxheight" default="300" />
		<cfargument name="maxwidth" default="300" />
		<cfscript>
			var imagecfc = getImagecfc();
			var info = imagecfc.getImageInfo("","#arguments.filepath#");
			var results = "";
			if(info.height gt maxheight OR info.width gt maxwidth) {
				results = imagecfc.resize("","#arguments.filepath#","#arguments.filepath#", 200, 200, true);
			}
			if(isStruct(results) and results.errorMessage neq "") {
			  dumpvar(results);
			}
		</cfscript>
	</cffunction>

	<cffunction name="fileupload" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" required="true">
		<cfargument name="fileField" required="true">
		<cfargument name="commitMessage" default="Adding new file">
		<cfset var url = "">
		<cffile action="upload"
		fileField = "#fileField#"
		destination="/tmp/"
		nameConflict = "overwrite">
		<!--- read it into a variable --->
		<cfif lcase(cffile.contentType) eq "image">
		  <cfset shrinkImageIfNeeded("/tmp/#cffile.serverFile#") />
		</cfif>
		<cffile action = "readbinary" file = "/tmp/#cffile.serverFile#"
		variable = "fileData">
		<cfset url = currentFolder & file.ServerFileName & "." & file.ServerFileExt>
		<cfset getSvnService().addOrUpdateResource(url,commitMessage,fileData)>
		<cfset getSvnService().export(url)>
		<cfoutput>
 		<script type="text/javascript">
		window.parent.frames['frmUpload'].OnUploadCompleted(<cfoutput>0</cfoutput>) ;
		</script>
		<cfabort>
		</cfoutput>
	</cffunction>

	<cffunction name="ExportResource" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfset var newFolder = arguments.currentFolder>
	  <cfset var daError = 0>
			<cftry>
		  <cfset newFolder = getSvnService().export(newFolder)>
			<!--- 	
			Possible Error Numbers are:
			o 0 : No Errors Found. The folder has been created.
			o 101 : Folder already exists.
			o 102 : Invalid folder name.
			o 103 : You have no permissions to create the folder.
			o 110 : Unknown error creating folder.
			 --->
<!---
 				<cfoutput>
					1#CFCATCH.Type#<br>
					2#CFCATCH.Message#<br>
					2#CFCATCH.detail#<br>
					3#daError#
					4#CFCATCH.ErrorCode#<br>
				</cfoutput><br>	
				<cfabort>
 --->
				<cfcatch>
					<cfif findNoCase('File already exists',cfcatch.message)>
						<cfset daError = 101>
					<cfelse>
						<cfdump var="#cfcatch#"><cfabort>
					</cfif>
				</cfcatch>
			</cftry>
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="ExportResource" resourceType="File">
			<CurrentFolder path="#currentFolder#" url="#CurrentFolder#" />'>
			<cfset daFile = daFile & '<Error number="#daError#" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>

	<cffunction name="GetUploadProgress" access= "public" returntype="any" output="false">
		<cfargument name="currentFolder" default="/">
		<cfargument name="resourceType" default="/">
		<cfset daFile = '<?xml version="1.0" encoding="utf-8" ?> 
		<Connector command="GetUploadProgress" resourceType="#arguments.resourceType#">
			<CurrentFolder path="#arguments.currentFolder#" url="#arguments.currentFolder#" />'>
			<cfset daFile = daFile & '<Progress max="2" value="2" />'>
			<cfset daFile = daFile & '<RefreshURL url="" />'>
		<cfset daFile = daFile & '</Connector>'>
	  <cfreturn daFile>
	</cffunction>

</cfcomponent>